package com.daoeping.dao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.filter.OncePerRequestFilter;




/**
 * 数据库操作接口实现类 DAO层
 * 
 * @author Rongqing Zhao
 * 
 */
public class DataBaseDaoImpl implements DataBaseDao {
	
	protected static final Log log = LogFactory
			.getLog(DataBaseDaoImpl.class);
	protected SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	@Transactional
	public boolean insertData(Object obj) {
		log.debug("operation:insert");
		try {
			sessionFactory.getCurrentSession().save(obj);
			return true;
		} catch (Exception e) {
			return false;
		}
		
	}

	@Override
	@Transactional
	public boolean deleteData(Object obj) {
		log.debug("operation:delete");
		try {
			sessionFactory.getCurrentSession().delete(obj);
			return true;
		} catch (Exception e) {
			return false;
		}
		
	}

	@Override
	@Transactional
	public boolean updateData(Object obj) {
		log.debug("operation:update");
		try {
			sessionFactory.getCurrentSession().update(obj);
			return true;
		} catch (Exception e) {
			return false;
		}
		
	}

	@Override
	@Transactional
	public Object selectDataByHQL(String hql) {

		/*
		 * Query query1 =sessionFactory.getCurrentSession().createQuery(
		 * "select Object(o) from User o"); query1.setCacheable(true); List
		 * list1 = query1.list(); System.out.println("list1=="+list1);
		 * 
		 * Query query2
		 * =sessionFactory.openSession().createQuery("select Object(o) from User o"
		 * ); query2.setCacheable(true); List list2 = query2.list();
		 * System.out.println("list2=="+list2); Query query3
		 * =sessionFactory.openSession
		 * ().createQuery("select Object(o) from User o");
		 * query3.setCacheable(true); List list3 = query3.list();
		 * System.out.println("list3=="+list3);
		 */
		System.out.println("operation:select,hql:"+hql);
		log.debug("operation:select,hql:"+hql);
		try {
			return sessionFactory.getCurrentSession().createQuery(hql).list();
		} catch (Exception e) {
			return false;
		}
		
	}

	@Override
	@Transactional
	public Object selectDataBySQL(String sql) {
		log.debug("operation:select,sql:"+sql);
		try {
			return sessionFactory.getCurrentSession().createSQLQuery(sql).list();
		} catch (Exception e) {
			return false;
		}
		
	}
	
	@Override
	@Transactional
	public boolean updateDataByHQL(String hql) {
		log.debug("operation:update,hql:"+hql);
		System.out.println(hql);
		try {
			sessionFactory.getCurrentSession().createQuery(hql).executeUpdate();
			return true;
		} catch (Exception e) {
			return false;
		}
		
	}

	@Override
	@Transactional
	public Object pageDataByHQL(String hql, int offSet, int pageSize) {
		System.out.println("operation:select,hql:"+hql);
		log.debug("operation:select,hql:"+hql+",offSet:"+offSet+",pageSize:"+pageSize);
		try {
			return sessionFactory.getCurrentSession().createQuery(hql).setFirstResult(offSet).setMaxResults(pageSize).list();
		} catch (Exception e) {
			return false;
		}
		
	}
	

}
